<%--
  Created by IntelliJ IDEA.
  User: 21933
  Date: 2021/1/19
  Time: 16:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>
<%--头部搜索栏--%>
<div class="demoTable">
    <form>
        <div class="layui-inline">
            <input type="text" class="layui-input" name="id" id="topAuthor"   autocomplete="off" placeholder="请输入作者">
        </div>
        <div class="layui-inline">
                <div class="layui-input-inline">
                    <select id="typeSelect" name="typeid" lay-filter="type">
                        <option value="">请选择书籍类型</option>
                    </select>
                </div>
        </div>
        <button class="layui-btn" id="reloads">搜索</button>
    </form>
</div>

<%--t_book表--%>
        <table class="layui-hide" id="allBook" lay-filter="allBook"></table>
<%--头部工具栏--%>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="createOutTable">加入出库申请单</button>
            </div>
        </script>
<%--outApply的头部工具栏--%>
    <script type="text/html" id="outApplylbar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="outApply">出库</button>
        </div>
    </script>

<%--申请发货页面不用操作列表--%>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>

<%--出库申请单--%>
        <table class="layui-hide" id="outApply" lay-filter="outApply"></table>
</body>
<script>
    layui.use(['table','jquery','layer','form'], function(){
        var table = layui.table;
        var $=layui.jquery;
        var layer=layui.layer;
        var form=layui.form;
        //页面加载完成后执行的事情
        $(function () {
            getType();
            allbook();
        });

        //数据验证
       $("#topAuthor").change(function () {
           var reg=/^[\u4e00-\u9fa5]{0,}$/;
           var msg=$("#topAuthor").val();
           if(!reg.test(msg)) {
               layer.msg("只能输入汉字");
           }
       });

        //用于接收库存表的数据，向出库表渲染
        var dataAll=[];

        //查询书本类型库
        function getType(){
            $.ajax({
                type:'get',
                url:'${pageContext.request.contextPath}/store/getType',
                dataType:'json',
                success:function (typedata) {
                    for(var i=0;i<typedata.length;i++){
                        $("#typeSelect").append(
                            $("<option>").attr("value",typedata[i][0]).html(typedata[i][1])
                        )
                    }
                }
            })
        }

        //库存表格
        function allbook(){
            table.render({
                elem: '#allBook'
                ,url:'${pageContext.request.contextPath}/book/showAllBook'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,title: '书本库存'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'bookid', title:'ID',fixed: 'left', unresize: true, sort: true}
                    ,{field:'bookname', title:'书名'}
                    ,{field:'author', title:'作者'}
                    ,{field:'count', title:'总数'}
                    ,{field:'remark', title:'综述', sort: true}
                    ,{field:'typeid', title:'类型编号'}
                    ,{field:'publisher', title:'出版社'}
                    ,{field:'price', title:'单价', sort: true}
                    ,{field:'status', title:'在售状态',templet:function (d) {
                            if (d.status==0){
                                return "<button class='layui-btn layui-btn-xs layui-btn-danger'>上架</button>";
                            }else{
                                return "<button class='layui-btn layui-btn-xs layui-btn-normal'>下架</button>";
                            }
                        }}
                    ,{field:'typename', title:'类型'}
                    // ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]]
                ,page: true
                ,limit:5 //设置一页几条
                ,limits:[5,10,15] //放置可供
            });
        }

        //搜索框
        $("#reloads").click(function () {
            //获得作者的写入框
            var author=$("#topAuthor");
            //获得类型的选择框
            var bookType=$("#typeSelect");
            //执行重载
            table.reload('allBook', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    author: author.val(),
                    typeid:bookType.val()
                }
            });
        })

        // 监听出库表单的头工具栏事件
        table.on('toolbar(outApply)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'outApply':
                    var outDatas = checkStatus.data;
                    $.ajax({
                        type:'post',
                        url:'${pageContext.request.contextPath}/store/out',
                        data:JSON.stringify(outDatas),
                        contentType:'application/json',
                        dataType: 'json',
                        success:function (datasu) {
                            if(datasu.code==1){
                                layer.closeAll();
                                //重载库存表
                                allbook();
                                dataAll=[];
                                //重载出库申请表
                                outApply();
                                layer.msg(datasu.msg,{icon:1});
                            }else {
                                layer.msg(datasu.msg,{icon:5});
                            }
                        }
                    })
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //去重
        function delSame(points) {
            var result = [];
            var obj = {};
            for (var i = 0; i < points.length; i++) {
                if (!obj[points[i].bookid]) {
                    result.push(points[i]);
                    obj[points[i].bookid] = true;
                }
            }
            return result;
        }

        //头工具栏事件
        table.on('toolbar(allBook)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'createOutTable':
                    var checkData = checkStatus.data;
                    if (checkData.length > 0) {
                        for (let i = 0; i < checkStatus.data.length; i++) {
                            checkStatus.data[i].count = 0;
                            dataAll.push(checkStatus.data[i]);
                        }
                        dataAll = delSame(dataAll);
                    } else {
                        layer.msg('选内容', {icon: 5});
                    }
                    outApply();
                    break;
                case 'addstaff':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //出库表单
        function outApply(){
            //id=outApply
            table.render({
                elem: '#outApply'
                ,toolbar: '#outApplylbar' //开启头部工具栏，并为其绑定左侧模板
                ,title: '出库申请单'
                ,data:dataAll
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'bookid', title:'ID',fixed: 'left', unresize: true, sort: true}
                    ,{field:'bookname', title:'书名'}
                    ,{field:'author', title:'作者'}
                    ,{field:'count', title:'总数',edit: 'text'}
                    ,{field:'remark', title:'综述', sort: true}
                    ,{field:'typeid', title:'类型编号'}
                    ,{field:'publisher', title:'出版社'}
                    ,{field:'price', title:'单价', sort: true}
                    ,{field:'status', title:'在售状态',templet:function (d) {
                            if (d.status==0){
                                return "<button class='layui-btn layui-btn-xs layui-btn-danger'>下架</button>";
                            }else{
                                return "<button class='layui-btn layui-btn-xs layui-btn-normal'>上架</button>";
                            }
                        }}
                    ,{field:'typename', title:'类型'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]]
                ,page: true
                ,limit:5 //设置一页几条
                ,limits:[5,10,15] //放置可供
            });
        }

        //监听单元格的编辑
        table.on('edit(outApply)',function (obj) {
            // alert(obj.value);
            $.ajax({
                type:'post',
                url:'${pageContext.request.contextPath}/book/bookcount',
                data:JSON.stringify(obj.data),
                contentType: 'application/json',
                dataType:'json',
                success:function (returnData) {
                    if(obj.value>returnData.count){
                        layer.alert("申请数量超出库存量",{icon:5});
                    }
                }
            })
        })

        //监听行工具事件
        table.on('tool(outApply)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });
    });


</script>
</html>
